#!/bin/sh

# version $Id: postinst 95 2009-12-12 17:57:47Z FR10848 $

PKG_SRC="/var/packages/$SYNOPKG_PKGNAME"
DESKTOP="/usr/syno/synoman/webman/3rdparty/$SYNOPKG_PKGNAME"
PHP_DIR="/usr/syno/etc"


# common
chown -R admin.users $SYNOPKG_PKGDEST

# Register as service
rm -rf /usr/syno/synoman/phpsrc/$SYNOPKG_PKGNAME
mkdir  /usr/syno/synoman/phpsrc/$SYNOPKG_PKGNAME
mv ${SYNOPKG_PKGDEST}/httpd/ /usr/syno/synoman/phpsrc/$SYNOPKG_PKGNAME/
mv ${SYNOPKG_PKGDEST}/data/ /usr/syno/synoman/phpsrc/$SYNOPKG_PKGNAME/


# webman
ln -s ${SYNOPKG_PKGDEST}/webman /usr/syno/synoman/webman/3rdparty/${SYNOPKG_PKGNAME}

# Remove previous Virtual Host listenning on 26180
sed -n '1h;1!H;${;g;s/<VirtualHost \*:26180.*<\/VirtualHost>//g;p;}' $PHP_DIR/httpd-vhost.conf-user > $PHP_DIR/vhost.tmp && mv -f $PHP_DIR/vhost.tmp $PHP_DIR/httpd-vhost.conf-user
sed 's/.*26180$//g' $PHP_DIR/httpd-vhost.conf-user > $PHP_DIR/vhost.tmp && mv -f $PHP_DIR/vhost.tmp $PHP_DIR/httpd-vhost.conf-user

# variable with content of the Open_basedir value from php.ini
OPENBASEDIR=`sed -e "/^open_basedir/!d" $PHP_DIR/php.ini |  sed -e "s/^open_basedir\s=\s\(.*\)$/\1/"`

echo "Opendir=$OPENBASEDIR et php=$PHP_DIR" >/tmp/debug
# configure virtual host
sed `echo "s/SYNOPKG_PKGDEST/$(echo "${SYNOPKG_PKGDEST}" | sed 's/\//\\\\\//g')/g"` ${SYNOPKG_PKGDEST}/conf/httpd-synoneuf.conf-template | sed 's#OPEN_BASEDIR#'"${OPENBASEDIR}"'#' > ${SYNOPKG_PKGDEST}/conf/httpd-synoneuf.conf-user

# configure start-stop-status script for restart bug syno, allow to "hard coding" SYNOPKG_PKGDEST variable into start-stop-status script
sed `echo "s/__SYNOPKGPKGDEST__/$(echo "${SYNOPKG_PKGDEST}" | sed 's/\//\\\\\//g')/g"` $PKG_SRC/scripts/start-stop-status > $PKG_SRC/scripts/start-stop-status.tmp && mv -f $PKG_SRC/scripts/start-stop-status.tmp $PKG_SRC/scripts/start-stop-status
chmod 755 $PKG_SRC/scripts/*

# end
exit 0
